package test;
/***********************************************************************/
/* Copyright (c) 1999-2005, The Rector and Board of Visitors of the    */
/* University of Virginia.                                             */
/* All rights reserved.                                                */
/*                                                                     */
/* This program is being made available under the terms of version 2.1 */
/* of theGNU Lesser General Public License ("LGPL") as published as of */
/* February, 1999 by the Free Software  Foundation; and subject to the */
/* restrictions of the LGPL, you are authorized to  modify and/or      */
/* redistribute it.                                                    */
/*                                                                     */
/* As stated in the LGPL, THIS PROGRAM IS BEING MADE AVAILABLE "AS IS",*/
/* AND WITHOUT A WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,    */
/* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF            */
/* MERCHANTABILITY AND  FITNESS FOR A PARTICULAR PURPOSE. FURTHER, YOU */
/* AGREE NOT TO HOLD THE COPYRIGHT  OWNER(S) LIABLE FOR DAMAGES,       */
/* INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,*/
/* ARISING OUT OF THE USE OR INABILITY TO USE  THE PROGRAM.            */
/* See the GNU General Public License for more details.                */
/*                                                                     */
/* You should have received a copy of version 2.1 of the GNU General   */
/* Public License along with this program; if not, you may obtain a    */
/* copy  from the Free Software Foundation website at the following URL*/
/* address (link  active as of September 1, 2005):                     */
/* http://www.fsf.org/licensing/licenses/lgpl.html; or that failing,   */
/* you can request a copy from Free Software Foundation,  Inc., 51     */
/* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.                */
/*                                                                     */
/* This program is part of the HyperCast software distribution. To     */
/* learn more about HyperCast, please visit the website at             */
/* http://www.hypercast.org.                                           */
/***********************************************************************/

/** "Hello, world." program for HyperCast 2.0, modified
 *
 * @author HyperCast Team 
 * @version Hello World (Callback Version) 1.0, Nov 2001
 */

import hypercast.*;
import hypercast.events.*;

public class HelloWorld implements I_ReceiveCallback
{ 

	public void ReceiveCallback(I_OverlayMessage RecvdMessage)
	{
	     /* get the logical address of the sender */ 
	     String SenderSrc = RecvdMessage.getSourceAddress().toString();
	
	     /* Next we transfer the received message into a string */
	     String sMsg=new String(RecvdMessage.getPayload(), 0, 
	     RecvdMessage.getPayloadLength());
	
	     /* ... and print the payload */ 
		 //if(!RecvdMessage.getSrcLogicalAddress().equals(MySocket.getLogicalAddress()))
		 System.out.println("Received <"+ sMsg + ">from node with logical address: " +SenderSrc +" . ");
	}


	public static void main()
	{
		HelloWorld hw = new HelloWorld();
	
		NotificationHandler cnh = new myNotificationHandler ();
		String MyString = new String("Hello World");
	
		HyperCastConfig ConfObj = HyperCastConfig.createConfig("spt.xml");
	
		I_OverlaySocket MySocket=ConfObj.createOverlaySocket ((I_ReceiveCallback) hw, cnh);
	
		MySocket.joinOverlay();
	
		/*
		 * Application blocks here until event NODE_ISSTABLE occurs.
		 */
		//cnh.waitUntil_NODE_ISSTABLE();
	
		System.out.println("This node's logical address is:" +MySocket.getLogicalAddress().toString());

		/*
		 * Send message to other applications.
		 */	
	    I_OverlayMessage MyMessage = MySocket.createMessage(MyString.getBytes());
	    for(int i=1; i<=10; i++){
	    	MySocket.sendToAll(MyMessage);
	   	    System.out.println("<HelloWorld> message sent to other members ...");
	   	    try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {}
	    }
	    //MySocket.leaveOverlay();
	    //cnh.waitUntil_NODE_LEAVEOVERLAY();
	    System.exit(0);
	}

}

class myNotificationHandler extends NotificationHandler
{
	public void handle_NODE_ISSTABLE(NODE_ISSTABLE event)
	{
			System.out.println("event NODE_ISSTABLE has occured!");
	}
		
	public void handle_NODE_NEIGHBORHOODCHANGED(NODE_NEIGHBORHOODCHANGED event)
	{
		System.out.println("event NODE_NEIGHBORHOODCHANGED has occured!");
	}
	
	public void handle_E2EACK_RECEIVED(E2EACK_RECEIVED event)
	{
			System.out.println("event E2EACK_RECEIVED has occured!");
	}
	
	public void handle_E2EPARTIALACK_RECEIVED(E2EPARTIALACK_RECEIVED event)
	{
			System.out.println("event E2EPARTIALACK_RECEIVED has occured!");
	}
	
	public void handle_MSG_WITH_E2EACK_SENT(MSG_WITH_E2EACK_SENT event)
	{
			System.out.println("event MSG_WITH_E2EACK_SENT has occured!");
	}
	
	public void handle_INTERNAL_EVENT(INTERNAL_EVENT event)
	{
			System.out.println("event INTERNAL_EVENT has occured!");
	}
	
	public void handle_NAMING_EVENT(NAMING_EVENT event)
	{
			System.out.println("event NAMING_EVENT has occured!");
	}
	
	public void handle_NEWSTREAM_ARRIVED_EVENT (NEWSTREAM_ARRIVED_EVENT  event)
	{
			System.out.println("event NEWSTREAM_ARRIVED_EVENT  has occured!");
	}
	
	public void handle_NODE_LEAVEOVERLAY(NODE_LEAVEOVERLAY event)
	{
			System.out.println("event INTERNAL_EVENT has occured!");
	}
	
	public void handle_NODE_LOGICALADDRESSCHANGED(NODE_LOGICALADDRESSCHANGED event)
	{
			System.out.println("event NODE_LOGICALADDRESSCHANGED has occured!");
	}
	
	public void handle_NOTIFICATION_EVENT(NOTIFICATION_EVENT event)
	{
			System.out.println("event NOTIFICATION_EVENT has occured!");
	}
}